package com.zr.d_c.controller;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.zr.d_c.model.Dish;
import com.zr.d_c.model.Seller;
import com.zr.d_c.model.User;
import com.zr.d_c.service.DishService;
import com.zr.d_c.service.SellerService;
import com.zr.d_c.serviceimpl.DishServiceImpl;
import com.zr.d_c.serviceimpl.SellerServiceImpl;
/**
 * 商家单个增加菜品
 * @author Administrator
 *
 */
public class InsertDishController extends HttpServlet {
	DishService dishService = new DishServiceImpl();
	SellerService sellerService = new SellerServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = req.getSession();
		PrintWriter pw = resp.getWriter();	
		//1.得到FileItem的集合
		DiskFileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(factory);
		//设置上传最大大小
		upload.setSizeMax(1024 * 1024 * 5);
		upload.setHeaderEncoding("utf-8");
		try {
			User u = (User) session.getAttribute("currentUser");
			int sid = sellerService.getSellerSidByUid(u.getUid());
			List<FileItem> items = upload.parseRequest(req);
			//2.遍历iteams:若是一个一般的表单域，打印信息
			//若是文件域,则把文件保存在上传的地方 F:/eclipse/workspace/d_c/src/main/webapp/img
			String dname = items.get(0).getString("utf-8");
			String dprice = items.get(2).getString("utf-8");
			String dnum = items.get(1).getString("utf-8");
			if(dname.equals("")||dprice.equals("")||dnum.equals("")){
				pw.write("0");
				pw.close();
			}else{
				String fileName = items.get(3).getName();
				
				String dtemp1 = "/upload/"+fileName;

				InputStream in = items.get(3).getInputStream();
				byte[] buffer = new byte[1024];
				int len = 0;
				
				fileName = "C:/Users/Administrator/git/d_cCRM/src/main/webapp/dish/image/" + fileName;
				
				OutputStream out = new FileOutputStream(fileName);
				
				while((len = in.read(buffer)) != -1){
					out.write(buffer, 0, len);
				}
				out.close();
				in.close();
				
				Dish d = new Dish();
				Date date = new Date();	
				SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
				d.setDcreatetime(f.format(date));
				d.setDisdelete("1");
				d.setSid(sid);
				d.setDname(dname);
				d.setDnum(Integer.parseInt(dnum));
				d.setDprice(Integer.parseInt(dprice));
				d.setDtemp1(dtemp1);

				if(dishService.insertDish(d, sid)){
					pw.write("1");
				}else{
					pw.write("0");
				}
				pw.close();
			}
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
